﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VoiceAI;
using VoiceAI.VoiceResponse;
using Windows.Data.Json;

namespace XiaoweiVoiceAI.ResponseCreators
{
    internal static class RadioResponseCreator
    {
        private class RadioResponseData : IPlayControlResponseData
        {
            public PlayControlType PlayControlType { get; set; }

            public PlayerType PlayerType { get; set; }

            public IPlayableResource PlayableResource { get; set; }

            public string PlayTittle { get; set; }

            public object PlayStateAdditionalMessage { get; set; }

            public ResponseType ResponseType => ResponseType.PlayControl;

            public ErrorCode ErrorCode => ErrorCode.NoError;

            public string RequestText { get; set; }
            public string ResponseText { get; set; }
            public string TTSUrl { get; set; }

            public bool RestoreInHistory { get; set; }

            public object DataContext { get; set; }

            public int? ControlValue { get; set; }
        }

        private class RadioItem : IRadioItem
        {
            public string Source { get; set; }

            public string Title { get; set; }

            public string MediaUri { get; set; }

            public string CoverUrl { get; set; }

            public string Summary { get; set; }

            public string PlayId { get; set; }
        }

        private class RadioResource : IRadioResource
        {
            public IReadOnlyList<IRadioItem> RadioItemList { get; set; }

            public bool HasMorePlayResource { get; set; }

            public bool HasMorePlayResourceUp { get; set; }

            public PlaylistAction PlaylistAction { get; set; }
        }
        private static Dictionary<XiaoweiV2.CommandId, PlayControlType> commandIdMap = new Dictionary<XiaoweiV2.CommandId, PlayControlType>()
        {
            {XiaoweiV2.CommandId.Play, PlayControlType.Play},
            {XiaoweiV2.CommandId.Stop, PlayControlType.Stop},
            {XiaoweiV2.CommandId.Pause, PlayControlType.Pause},
            {XiaoweiV2.CommandId.Prev, PlayControlType.Prev},
            {XiaoweiV2.CommandId.Next, PlayControlType.Next},
            {XiaoweiV2.CommandId.AddOffset, PlayControlType.AddOffset},
            {XiaoweiV2.CommandId.SetOffsetTo, PlayControlType.SetOffsetTo},
            {XiaoweiV2.CommandId.PlayMode, PlayControlType.PlayMode},
        };
        private static Dictionary<XiaoweiV2.CommandId, string> responseTextMap = new Dictionary<XiaoweiV2.CommandId, string>()
        {
            {XiaoweiV2.CommandId.Play,"好的，开始播放" },
            {XiaoweiV2.CommandId.Stop,"好的，停止播放" },
            {XiaoweiV2.CommandId.Pause,"好的，暂停播放" },
            {XiaoweiV2.CommandId.Prev,"好的，开始播放上一首" },
            {XiaoweiV2.CommandId.Next,"好的，开始播放下一首" },
            {XiaoweiV2.CommandId.AddOffset,"好的，已重新定位" },
            {XiaoweiV2.CommandId.SetOffsetTo,"好的，已重新定位" },
            {XiaoweiV2.CommandId.PlayMode,"好的，已切换播放模式" }
        };

        public static IResponseData CreateResponseData(XiaoweiVoiceAI xiaoweiVoiceAI, XiaoweiV2.ResponseParam param)
        {
            RadioResponseData radioResponseData = new RadioResponseData();
            radioResponseData.RequestText = param.RequestText;
            radioResponseData.ResponseText = param.ResponseText;
            radioResponseData.TTSUrl = XiaoweiV2.ResponseHelper.GetTTSUrl(param);
            radioResponseData.RestoreInHistory = !string.IsNullOrEmpty(param.RequestText);
            radioResponseData.PlayStateAdditionalMessage = param.Skill;
            //radioResponseData.PlayStateAdditionalMessage=new 
            switch (param.Skill.Id)
            {
                case Skills.News:
                    radioResponseData.PlayerType = PlayerType.News;
                    break;
                case Skills.WeixinTingShu:
                    radioResponseData.PlayerType = PlayerType.Weixintingshu;
                    break;
                default:
                    radioResponseData.PlayerType = PlayerType.Radio;
                    break;
            }
            if (XiaoweiV2.ResponseHelper.IsCommand(param)&& param.ControlId!= XiaoweiV2.CommandId.PlayResource)
            {
                if (commandIdMap.ContainsKey(param.ControlId))
                {
                    radioResponseData.PlayControlType = commandIdMap[param.ControlId];
                    if (int.TryParse(param.ControlValue, out int value))
                    {
                        if (param.ControlId == XiaoweiV2.CommandId.PlayMode)
                        {
                            switch ((XiaoweiV2.PlayMode)value)
                            {
                                case XiaoweiV2.PlayMode.SequentialNoLoop:
                                    radioResponseData.ControlValue = (int)PlayMode.OrderPlay;
                                    break;
                                case XiaoweiV2.PlayMode.SequentialLoopList:
                                    radioResponseData.ControlValue = (int)PlayMode.LoopPlay;
                                    break;
                                case XiaoweiV2.PlayMode.ShuffleNoLoop:
                                    break;
                                case XiaoweiV2.PlayMode.ShuffleLoop:
                                    radioResponseData.ControlValue = (int)PlayMode.RandomPlay;
                                    break;
                                case XiaoweiV2.PlayMode.SingleLoop:
                                    radioResponseData.ControlValue = (int)PlayMode.SinglePlay;
                                    break;
                                default:
                                    break;
                            }
                        }
                        else
                        {
                            radioResponseData.ControlValue = value/1000;
                        }
                    }
                    else
                    {
                        radioResponseData.ControlValue = null;
                    }
                    if (!string.IsNullOrEmpty(param.RequestText))
                    {
                        radioResponseData.ResponseText = responseTextMap[param.ControlId];
                        radioResponseData.TTSUrl = xiaoweiVoiceAI.TextToVoice(radioResponseData.ResponseText);
                    }
                }
                else
                {
                    return null;
                }
            }
            else
            {
                radioResponseData.PlayControlType = PlayControlType.PlayResource;
                RadioResource radioResource = new RadioResource();
                //penguinFMList.Collection.Add
                var mediaExtras = XiaoweiV2.ResponseHelper.GetExtraInfo(param);
                if (mediaExtras.Count == 0) return null;
                string radioCover = "";
                radioResponseData.PlayTittle = param.Skill.Name;
                radioResource.HasMorePlayResource = param.HasMorePlaylist;
                radioResource.HasMorePlayResourceUp = param.HasMorePlaylistUp;
                List<IRadioItem> radioItems = new List<IRadioItem>();
                foreach (var info in mediaExtras)
                {
                    if (!String.IsNullOrEmpty(info.Cover))
                    {
                        radioCover = info.Cover;
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(radioCover))
                        {
                            info.Cover = radioCover;
                        }
                        else
                        {
                            info.Cover = "";
                        }
                    }
                    // check the url

                    if (!string.IsNullOrEmpty(info.Url) || param.Skill.Id == Skills.WeixinTingShu)
                    {
                        RadioItem radioItem;
                        if (param.Skill.Id == Skills.News)
                        {
                            radioItem = new RadioItem()
                            {
                                Source = info.Album,
                                Title = info.Name,
                                MediaUri = info.Url,
                                CoverUrl = info.Cover,
                                Summary = info.Summary,
                                PlayId = info.PlayId,
                            };
                        }
                        else
                        {
                            radioItem = new RadioItem()
                            {
                                Source = "",
                                Title = info.Name,
                                MediaUri = info.Url,
                                CoverUrl = info.Cover,
                                Summary = info.Album,
                                PlayId = info.PlayId,
                            };

                        }
                        radioItems.Add(radioItem);
                    }
                }
                radioResource.RadioItemList = radioItems;
                radioResponseData.PlayableResource = radioResource;
                switch (param.PlayBehavior)
                {
                    case XiaoweiV2.PlaylistAction.ReplaceAll:
                        radioResource.PlaylistAction = PlaylistAction.ReplaceAll;
                        break;
                    case XiaoweiV2.PlaylistAction.EnqueueFront:
                        radioResource.PlaylistAction = PlaylistAction.EnqueueFront;
                        break;
                    case XiaoweiV2.PlaylistAction.EnqueueBack:
                        radioResource.PlaylistAction = PlaylistAction.EnqueueBack;
                        break;
                    case XiaoweiV2.PlaylistAction.ReplaceEnqueue:
                        radioResource.PlaylistAction = PlaylistAction.ReplaceEnqueue;
                        break;
                    case XiaoweiV2.PlaylistAction.RefreshURL:
                        radioResource.PlaylistAction = PlaylistAction.RefreshURL;
                        break;
                    case XiaoweiV2.PlaylistAction.Remove:
                        radioResource.PlaylistAction = PlaylistAction.Remove;
                        break;
                    default:
                        break;
                }
            }
            return radioResponseData;
        }
    }
}
